package cn.qqtheme.framework.util;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import java.io.File;
import java.io.IOException;

/* loaded from: classes.dex */
public final class StorageUtils {
    public static boolean externalMounted() {
        if (Environment.getExternalStorageState().equals("mounted")) {
            return true;
        }
        LogUtils.warn("external storage unmounted");
        return false;
    }

    public static String getInternalRootPath(Context context, String str) {
        File file;
        if (TextUtils.isEmpty(str)) {
            file = context.getFilesDir();
        } else {
            File file2 = new File(FileUtils.separator(context.getFilesDir().getAbsolutePath()) + str);
            file2.mkdirs();
            file = file2;
        }
        String separator = file != null ? FileUtils.separator(file.getAbsolutePath()) : "";
        LogUtils.verbose("internal storage root path: " + separator);
        return separator;
    }

    public static String getInternalRootPath(Context context) {
        return getInternalRootPath(context, null);
    }

    public static String getExternalRootPath(String str) {
        File externalStorageDirectory = externalMounted() ? Environment.getExternalStorageDirectory() : null;
        if (externalStorageDirectory != null && !TextUtils.isEmpty(str)) {
            File file = new File(externalStorageDirectory, str);
            file.mkdirs();
            externalStorageDirectory = file;
        }
        String separator = externalStorageDirectory != null ? FileUtils.separator(externalStorageDirectory.getAbsolutePath()) : "";
        LogUtils.verbose("external storage root path: " + separator);
        return separator;
    }

    public static String getExternalRootPath() {
        return getExternalRootPath(null);
    }

    public static String getExternalPrivatePath(Context context, String str) {
        File externalFilesDir = externalMounted() ? context.getExternalFilesDir(str) : null;
        String separator = externalFilesDir != null ? FileUtils.separator(externalFilesDir.getAbsolutePath()) : "";
        LogUtils.verbose("external storage private path: " + separator);
        return separator;
    }

    public static String getExternalPrivatePath(Context context) {
        return getExternalPrivatePath(context, null);
    }

    public static String getDownloadPath() throws RuntimeException {
        if (!externalMounted()) {
            throw new RuntimeException("外置存储不可用！");
        }
        return FileUtils.separator(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
    }

    public static String getCachePath(Context context, String str) {
        File cacheDir;
        if (externalMounted()) {
            cacheDir = context.getExternalCacheDir();
        } else {
            cacheDir = context.getCacheDir();
        }
        if (!TextUtils.isEmpty(str)) {
            File file = new File(cacheDir, str);
            file.mkdirs();
            cacheDir = file;
        }
        String separator = cacheDir != null ? FileUtils.separator(cacheDir.getAbsolutePath()) : "";
        LogUtils.verbose("external or internal storage cache path: " + separator);
        return separator;
    }

    public static String getCachePath(Context context) {
        return getCachePath(context, null);
    }

    public static String getTempDirPath(Context context) {
        return getExternalPrivatePath(context, "temporary");
    }

    public static String getTempFilePath(Context context) {
        try {
            return File.createTempFile("lyj_", ".tmp", context.getCacheDir()).getAbsolutePath();
        } catch (IOException unused) {
            return getTempDirPath(context) + "lyj.tmp";
        }
    }
}
